/**
 * [LupMisNotAllowedSync]
 * 本代码为系统自动生成代码，请根据自己业务进行修改;
 * 生成时间 2022-02-02 11:18:21;
 * 版权所有 2020-2022 lizhongwen，并保留所有权利;
 * 说明: 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；不允许对程序代码以任何形式任何目的的再发布;
 * 作者: 中文Lee;
 * 作者主页: http://www.lizhongwen.com;
 * 邮箱: 360811363@qq.com;
 * QQ: 360811363;
 */

//这里引用一下，否则不生效
layui.use(['layer', 'table', 'form'], function() {
    ajaxStart();
    //统一给ajax请求加header参数
    ajaxSend();
    //截获ajax参数
    ajaxSetup();
});
$(document).ajaxSend(function(event, jqxhr, settings) {
    jqxhr.setRequestHeader("accesstoken", localStorage.getItem("accesstoken"));
    jqxhr.setRequestHeader("accessuid", localStorage.getItem("accessuid"));
});
var accessuid = localStorage.getItem("accessuid");
var accesstoken = localStorage.getItem("accesstoken");
var setting = {
    async: {
        enable: true,
        contentType: "application/json",
        headers: {
            'accessuid': accessuid,
            'accesstoken': accesstoken
        },
        type: "post",
        dataType: "json",
        autoParam: ["id", "name"],
        url: base_gateway + "sys/org/lists"
    },
    view: {
        expandSpeed: "1",
        showLine: true,
        showIcon: true,
        showTitle: true,
        selectedMulti: false
    },
    edit: {
        enable: false
    },
    data: {
        simpleData: {
            enable: true
        }
    },
    callback: {
        onClick: onClick,
        onAsyncSuccess: zTreeOnAsyncSuccess, // 异步加载正常结束的事件回调函数
    }
};

//用于捕获异步加载正常结束的事件回调函数
function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    var nodes = treeObj.getNodes();
    //默认展开前3个
    treeObj.expandAll(true);
    for (var i = 0; i < nodes.length; i++) {
        // treeObj.expandNode(nodes[i], true, false, false); //默认展开第一级节点
    }

}

function getChildren(ids, treeNode) {
    ids.push(treeNode.id);
    if (treeNode.isParent) {
        for (var obj in treeNode.children) {
            getChildren(ids, treeNode.children[obj]);
        }
    }
    return ids;
}

//目录点击事件
function onClick(event, treeId, treeNode, clickFlag) {
    var ids = [];
    ids = getChildren(ids, treeNode);
    $("input[name='org_id']").val(ids);
    $("button[type='submit']").click();
    //调用搜索
}

var treeDemoObj;
$(function() {
    treeDemoObj = $.fn.zTree.init($("#treeDemo"), setting);

    var windowH = $(window).height();
    if (windowH <= 300) {
        windowH = 300;
    }
    $("#right").css("height", (windowH - 125) + "px");
    $("#left").css("height", (windowH - 125) + "px");
    $("#treeDemo").css("height", (windowH - 135) + "px");
    $(window).resize(function() { //当浏览器大小变化时
        var windowH = $(window).height();
        if (windowH <= 300) {
            windowH = 300;
        }
        $("#right").css("height", (windowH - 125) + "px");
        $("#left").css("height", (windowH - 125) + "px");
        $("#treeDemo").css("height", (windowH - 135) + "px");
    });
});


function getRoleList() {
    var data = {
        "page": 1,
        "limit": 10000
    };

    request(base_gateway + 'sys/role/lists', data, function(ret) {
        var code = ret.code;
        var rows = ret.data.rows || ret.data;
        if (code == 200 && rows.length > 0) {
            var selectData = [];
            for (var i = 0; i < rows.length; i++) {
                var item = { name: rows[i].role_name, value: rows[i].id };
                selectData.push(item);
            }
            var roleSelectBox = xmSelect.render({
                el: '#roleSelectBox',
                filterable: true,
                tips: '用户角色',
                name: 'role_ids',
                on: function(data) {},
                data: selectData
            });
            layer.closeAll("loading");
        }
    });
}

layui.use(['layer', 'table', 'form'], function() {
    table = layui.table;
    form = layui.form;
    layui.$(document).on("click", "td div.laytable-cell-checkbox div.layui-form-checkbox", function(e) {
        e.stopPropagation();
    });
    //单击行勾选radio事件
    layui.$(document).on("click", ".layui-table-body table.layui-table tbody tr", function() {
        var index = layui.$(this).attr('data-index');
        var tableBox = layui.$(this).parents('.layui-table-box');
        //存在固定列
        var tableDiv;
        if (tableBox.find(".layui-table-fixed.layui-table-fixed-l").length > 0) {
            tableDiv = tableBox.find(".layui-table-fixed.layui-table-fixed-l");
        } else {
            tableDiv = tableBox.find(".layui-table-body.layui-table-main");
        }
        var checkCell = tableDiv.find("tr[data-index=" + index + "]").find("td div.laytable-cell-radio div.layui-form-radio I");
        if (checkCell.length > 0) {
            checkCell.click();
        }
    });

    layui.$(document).on("click", "td div.laytable-cell-radio div.layui-form-radio", function(e) {
        e.stopPropagation();
    });

    pageInit(function(pageInfo, toolbarHtml) {
        getRoleList();
        var jump = pageInfo.jump;
        var queryUrl = pageInfo.query_url;
        var tableName = pageInfo.table_name;
        var cols = [{
            type: 'radio'
        }, {
            field: 'id',
            title: 'ID',
            width: 265,
            hide: true
        }, {
            field: 'system_serial_number',
            title: '序号',
            width: 60,
            sort: true,
            align: 'center',
            type: 'numbers'
        }, {
            field: 'account_name',
            title: '账号',
            minWidth: 80,
            sort: true
        }, {
            field: 'role_id',
            title: '角色',
            minWidth: 70,
            templet: function(d) {
                if (!isEmpty(d.account_roles)) {
                    var _arr = [];
                    for (var i = 0; i < d.account_roles.length; i++) {
                        _arr[i] = d.account_roles[i].role_name
                    }
                    return _arr;
                } else {
                    return "";
                }

            },
            sort: false
        }, {
            field: 'full_name',
            title: '姓名',
            width: 80,
            sort: true
        }, {
            field: 'org_id',
            title: '机构',
            minWidth: 70,
            templet: function(d) {
                if (!isEmpty(d.org_row) && !isEmpty(d.org_row.name)) {
                    return d.org_row.name
                } else {
                    return "";
                }

            },
            sort: true
        }, {
            field: 'sex',
            title: '性别',
            width: 70,
            templet: function(d) {
                if (d.sex == "m") {
                    return "<span style='color:green'>男</span>"
                } else if (d.sex == "w") {
                    return "<span style='color:red'>女</span>";
                } else {
                    return "未知";
                }
            },
            sort: true
        }, {
            field: 'phone',
            title: '联系电话',
            width: 100,
            sort: true
        }];
        var rightH = $("#right").height();
        var h = rightH - 50;

        loadData(base_gateway + queryUrl, cols, toolbarHtml, null, h + "px");
        // 监听搜索操作
        doSearch();
        //监听操作事件(添加 删除 编辑)
        toolbarTap(function(data) {
            // console.log(JSON.stringify(data));
        });
        //监听单元格编辑
        tableEdit(tableName);
    });
});